package com.snap.vseries.fragment;

import android.annotation.SuppressLint;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.snap.vseries.AppConfig;
import com.snap.vseries.R;
import com.snap.vseries.base.BaseFragment;
import com.snap.vseries.bean.ExpInfo;
import com.snap.vseries.bean.HoleInfo;
import com.snap.vseries.utils.ToastUtils;

import butterknife.BindView;
import butterknife.BindViews;

/**
 * 实验结果：分析
 * 原始荧光、相对荧光、温度曲线
 */
@SuppressLint("ValidFragment")
public class DetailAnalyzeFragment extends BaseFragment implements RadioGroup.OnCheckedChangeListener {
    @BindViews({R.id.fam_cb, R.id.vic_cb, R.id.rox_cb, R.id.cy5_cb})
    CheckBox[] checkBoxes;
    @BindView(R.id.group_ll)
    LinearLayout groupLl;
    @BindView(R.id.radio_group)
    RadioGroup radioGroup;
    private ExpInfo experimentInfo;
    private int currentTableCheckedId;
    private int currentChartIndex;
    private ChartTempFragment tempFragment;
    private ChartAmpOriginFragment ampOriginFragment;
    private ChartAmpFitFragment ampFitFragment;
    private ChartMeltingFragment meltingFragment;
    private int[] radioButtons;


    public DetailAnalyzeFragment(ExpInfo experimentInfo) {
        this.experimentInfo = experimentInfo;
    }

    @Override
    protected int layoutResId() {
        return R.layout.fragment_detail_analyze;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        if (savedInstanceState != null) {
            currentChartIndex = savedInstanceState.getInt("currentChartIndex");
            currentTableCheckedId = savedInstanceState.getInt("currentTableCheckedId");
        }
        initViews();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (getActivity() == null) return;
        if (hidden) {
            getActivity().findViewById(R.id.protocol_btn).setVisibility(View.GONE);
        } else {
            getActivity().findViewById(R.id.protocol_btn).setVisibility(View.VISIBLE);
        }
    }

    public void setGroupLlVisibility(int value) {
        groupLl.setVisibility(value);
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        outState.putInt("currentChartIndex", currentChartIndex);
        outState.putInt("currentTableCheckedId", currentTableCheckedId);
        super.onSaveInstanceState(outState);
    }

    private void initViews() {
        HoleInfo holeInfo = experimentInfo.getHoleInfo();
        if (holeInfo == null) {
            ToastUtils.get().showText("警告：没有通道参与实验");
            return;
        }
        initCheckBox();
        showChartFragment(1);
        radioButtons = new int[]{R.id.radio1, R.id.radio2, R.id.radio3, R.id.radio4};
        radioGroup.setOnCheckedChangeListener(this);
    }


    private void showChartFragment(int chartIndex) {
        if (currentChartIndex == chartIndex) {
            return;
        }
        currentChartIndex = chartIndex;

        FragmentManager fm = getChildFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();

        ampOriginFragment = (ChartAmpOriginFragment) fm.findFragmentByTag(ChartAmpOriginFragment.class.getSimpleName());
        ampFitFragment = (ChartAmpFitFragment) fm.findFragmentByTag(ChartAmpFitFragment.class.getSimpleName());
        tempFragment = (ChartTempFragment) fm.findFragmentByTag(ChartTempFragment.class.getSimpleName());
        meltingFragment = (ChartMeltingFragment) fm.findFragmentByTag(ChartMeltingFragment.class.getSimpleName());

        switch (currentChartIndex) {
            case 2:
                if (tempFragment != null) {
                    ft.hide(tempFragment);
                }
                if (meltingFragment != null) {
                    ft.hide(meltingFragment);
                }
                if (ampOriginFragment != null) {
                    ft.hide(ampOriginFragment);
                }
                if (ampFitFragment == null) {
                    ampFitFragment = new ChartAmpFitFragment(experimentInfo);
                    ft.add(R.id.chart_container, ampFitFragment, ChartAmpFitFragment.class.getSimpleName());
                } else {
                    ft.show(ampFitFragment);
                }
                break;
            case 3:
                if (ampOriginFragment != null) {
                    ft.hide(ampOriginFragment);
                }
                if (ampFitFragment != null) {
                    ft.hide(ampFitFragment);
                }
                if (meltingFragment == null) {
                    meltingFragment = new ChartMeltingFragment(experimentInfo);
                    ft.add(R.id.chart_container, meltingFragment, ChartMeltingFragment.class.getSimpleName());
                } else {
                    ft.show(meltingFragment);
                }
                break;
            case 4:
                if (ampOriginFragment != null) {
                    ft.hide(ampOriginFragment);
                }
                if (ampFitFragment != null) {
                    ft.hide(ampFitFragment);
                }
                if (meltingFragment != null) {
                    ft.hide(meltingFragment);
                }
                if (tempFragment == null) {
                    tempFragment = new ChartTempFragment(experimentInfo);
                    ft.add(R.id.chart_container, tempFragment, ChartTempFragment.class.getSimpleName());
                } else {
                    ft.show(tempFragment);
                }
                break;
            default:
                if (tempFragment != null) {
                    ft.hide(tempFragment);
                }
                if (ampFitFragment != null) {
                    ft.hide(ampFitFragment);
                }
                if (meltingFragment != null) {
                    ft.hide(meltingFragment);
                }
                if (ampOriginFragment == null) {
                    ampOriginFragment = new ChartAmpOriginFragment(experimentInfo);
                    ft.add(R.id.chart_container, ampOriginFragment, ChartAmpOriginFragment.class.getSimpleName());
                } else {
                    ft.show(ampOriginFragment);
                }
        }
        ft.commitAllowingStateLoss();
    }


    private void initCheckBox() {
        HoleInfo holeInfo = experimentInfo.getHoleInfo();
        if (holeInfo.isFamEnable()) {
            checkBoxes[0].setChecked(true);
        }
        if (holeInfo.isVicEnable()) {
            checkBoxes[1].setChecked(true);
        }

        if (holeInfo.isRoxEnable()) {
            checkBoxes[2].setChecked(true);
        }

        if (holeInfo.isCy5Enable()) {
            checkBoxes[3].setChecked(true);
        }

        CompoundButton.OnCheckedChangeListener onCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                onHoleChannelChanged();
            }
        };
        for (CheckBox checkBox : checkBoxes) {
            if (!checkBox.isChecked()) {
                checkBox.setEnabled(false);
            }
            checkBox.setOnCheckedChangeListener(onCheckedChangeListener);
        }
    }


    private void onHoleChannelChanged() {
        HoleInfo holeInfo = experimentInfo.getHoleInfo();
        int channelFlag = 0;
        if (checkBoxes[0].isChecked()) {
            channelFlag |= 0x01;
        }
        if (checkBoxes[1].isChecked()) {
            channelFlag |= 0x02;
        }
        if (checkBoxes[2].isChecked()) {
            channelFlag |= 0x04;
        }
        if (checkBoxes[3].isChecked()) {
            channelFlag |= 0x08;
        }

        boolean[] bs = new boolean[AppConfig.TOTAL_CHANNEL_COUNT];
        for (int i = 0; i < AppConfig.TOTAL_CHANNEL_COUNT; i++) {
            bs[i] = checkBoxes[i].isChecked() && ((holeInfo.getChannelJoinFlag() >> i & 0x01) == 0x01);
        }
        onHoleChannelChanged(bs, channelFlag);
    }

    public Bundle getChooseInfo() {
        HoleInfo holeInfo = experimentInfo.getHoleInfo();
        int channelFlag = 0;
        if (checkBoxes[0].isChecked()) {
            channelFlag |= 0x01;
        }
        if (checkBoxes[1].isChecked()) {
            channelFlag |= 0x02;
        }
        if (checkBoxes[2].isChecked()) {
            channelFlag |= 0x04;
        }
        if (checkBoxes[3].isChecked()) {
            channelFlag |= 0x08;
        }
        boolean[] bs = new boolean[AppConfig.TOTAL_CHANNEL_COUNT];
        for (int i = 0; i < AppConfig.TOTAL_CHANNEL_COUNT; i++) {
            bs[i] = checkBoxes[i].isChecked() && ((holeInfo.getChannelJoinFlag() >> i & 0x01) == 0x01);
        }
        Bundle bundle = new Bundle();
        bundle.putBooleanArray("check", bs);
        bundle.putInt("channelFlag", channelFlag);
        return bundle;
    }


    private void onHoleChannelChanged(boolean[] bs, int channelFlag) {
        if (ampOriginFragment != null) {
            ampOriginFragment.setShowLines(bs, channelFlag);
        }
        if (ampFitFragment != null) {
            ampFitFragment.setShowLines(bs, channelFlag);
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (radioButtons != null && radioButtons.length > 0) {
            for (int id : radioButtons) {
                if (id == checkedId) {
                    ((RadioButton) group.findViewById(id)).setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                } else {
                    ((RadioButton) group.findViewById(id)).setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                }
            }
        }
        switch (checkedId) {
            case R.id.radio1:
                showChartFragment(1);
                break;
            case R.id.radio2:
                showChartFragment(2);
                break;
            case R.id.radio3:
                showChartFragment(3);
                break;
            case R.id.radio4:
                showChartFragment(4);
                break;
            default:
        }

    }

    public View getAmpOriginView() {
        return ampOriginFragment.getAmpOriginView();
    }
}